// Copyright 2013 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris	polaris@studygolang.com

package util

// 每页显示多少条
const PerPage = 10

// Paginator 分页器
type Paginator struct {
	curPage int
	perPage int

	total     int
	totalPage int
}

func NewPaginator(curPage int) *Paginator {
	return NewPaginatorWithPerPage(curPage, PerPage)
}

func NewPaginatorWithPerPage(curPage, perPage int) *Paginator {
	return &Paginator{curPage: curPage, perPage: perPage}
}

const maxShow = 5

func (this *Paginator) SetTotal(total int64) *Paginator {
	this.total = int(total)
	if this.perPage != 0 {
		this.totalPage = this.total / this.perPage
		if this.total%this.perPage != 0 {
			this.totalPage++
		}
	}
	return this
}

func (this *Paginator) GetTotal() int {
	return this.total
}

func (this *Paginator) GetTotalPage() int {
	return this.totalPage
}

func (this *Paginator) Offset() (offset int) {
	if this.curPage > 1 {
		offset = (this.curPage - 1) * this.perPage
	}
	return
}

func (this *Paginator) SetPerPage(perPage int) {
	this.perPage = perPage
}

func (this *Paginator) PerPage() int {
	return this.perPage
}
func (this *Paginator) CurPage() int {
	return this.curPage
}

func (this *Paginator) HasMorePage() bool {
	return this.totalPage > this.curPage
}

// 计算页面工具
func TotalPage(rows, total int64) int64 {
	var result int64
	result = total % rows
	if result != 0 {
		pages := total / rows
		pages = pages + 1
		return pages
	} else {
		pages := total / rows
		return pages
	}
}
